package baseStructure;

/***
 * @author Ragnvald Fuglestein
 */

import java.io.Serializable;

public final class Album extends Base implements Serializable {
	private static final long serialVersionUID = 1958925052586610001L;
	private Artist artist = null;
	private final SortedList<Track> tracks = new SortedList<>(20, 3);
	
	private int year = -1;
	
	public Album(final Artist artist, final String name) {
		super(name);
		this.artist = artist;
	}
	
	public Album(final Artist artist, final String name, final int year) {
		super(name);
		this.artist = artist;
		setYear(year);
	}
	
	public final Artist getArtist() {
		return artist;
	}
	
	public final void setArtist(Artist artist) {
		this.artist = artist;
	}

	/*** 
	 * 
	 * @return Year or -1 if unset
	 */
	public final int getYear() {
		return year;
	}
	
	/***
	 * 
	 * @return year as string or "" if unset
	 */
	public final String getYearStr() {
		if (year == -1) return "";
		return "" + year;
	}
	
	/***
	 * 
	 * @param Year
	 */
	public final void setYear(final int year) {
		this.year = year;
		notifyElementChangedListener();
	}

	/***
	 * 
	 * @param track
	 * @return true if list changed as a result of the method call
	 */
	public final boolean addTrack(final Track track) {
		if (!this.tracks.contains(track)) {
			this.tracks.add(track);
			track.setAlbum(this);
			track.setArtist(artist);
			return true;
		}
		return false;
	}

	/***
	 * 
	 * @param track
	 * @return true if list changed as a result of the method call
	 */
	public final boolean removeTrack(final Track track) {
		if (this.tracks.remove(track)) {
			return true;
		}
		return false;
	}

	/***
	 * @return List of all tracks
	 */
	public SortedList<Track> getTracks() {
		return this.tracks;
	}

	/***
	 * Add tracks matching search to list 
	 * @param list search
	 */
	public void getTracks(SortedList<Track> list, SearchTrack search) {
		tracks.getMatch(list, search);
	}
	
	/***
	 * Merges all tracks in album to this object
	 * @param album
	 * @return result of Base.merge
	 */
	public boolean merge(final Album album) {
		SortedList<Track> tl = new SortedList<>();
		for (Track track : album.getTracks()) {
			tracks.getMatch(tl, track.toSearch());
			if (tl.size() > 0) {
				for (Track t : tl) {
					t.merge(track);
				}
			} else {
				addTrack(track);
			}
		}
		return super.merge(album);
	}
	
	/***
	 * 
	 * @return SearchAlbum object with albumname and year(if set)
	 */
	public SearchAlbum toSearch() {
		if (year != -1 ) return new SearchAlbum(getName(), year);
		return new SearchAlbum(getName());
	}
	
	public String toString() {
		if (year == -1) return getName();
		return year + " - " + getName();
	}
	
	public String getInfo() {
		String ret = "";
		
		ret += "Artist name: " + getArtist().getName() + "\n";
		ret += "Album name: " + getName() + "\n";
		if (year > 0) ret += "Album Year: " + getYearStr() + "\n";
		ret += super.getInfo();
		return ret;
	}
}